package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());
        String[] splits = ex.getMessage().split("'");
        return R.error(splits[1]+"已存在");
    }

    @ExceptionHandler(MyException.class)
    public  R<String> exceptionHandler(MyException ex){
        return R.error(ex.getMessage());
    }
}
